package com.shiyi.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shiyi.common.Constants;
import lombok.extern.slf4j.Slf4j;

import static com.shiyi.common.Constants.TIAN_KEY;

/**
 * @author: muxiongxiong
 * @date: 2022年12月19日 22:36
 * 公众号：雄雄的小课堂
 * 博客：https://blog.csdn.net/qq_34137397
 * 个人站：http://www.穆雄雄.com
 * 个人站：http://www.muxiongxiong.cn
 * @Description: 天行api接口调用
 */
@Slf4j
public class TianXingApiUtil {

    /**
     * 获取每日早安寄语
     *
     */
    public static String getGoodMorning(String tx_key) {
        String path = "zaoan/index";
        //调用每日简报的接口
        String body = HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + tx_key)
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if (code != 200) {
            log.error("请求每日新闻的接口出错，出错信息：" + body);
            return "一切都是最好的安排~";
        }
        log.info("请求每日新闻的接口成功：" + body);
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        return resultObject.getString("content");
    }

    /**
     * 获取每日土味情话
     *
     */
    public static String getLoveWords(String tx_key){
        String path = "saylove/index";
        //调用每日简报的接口
        String body = HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + tx_key)
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return "一切都是最好的安排~";
        }
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        log.info("请求每日新闻的接口成功："+resultObject);
        return resultObject.getString("content");
    }


    /**
     * 获取天气预报
     *
     */
    public static JSONObject getWeather(String tx_key){
        String path = "tianqi/index";
        //调用每日简报的接口
        String body = HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" +  tx_key+"&type=1&city=370100")
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return null;
        }

        return jsonObjectResult.getJSONObject("result");
        /*
        * {
          "code": 200,
          "msg": "success",
          "result": {
            "date": "2022-12-19",
            "week": "星期一",
            "province": "上海",
            "area": "上海",
            "areaid": "101020100",
            "weather": "晴",
            "weatherimg": "qing.png",
            "weathercode": "qing",
            "real": "3℃",
            "lowest": "2℃",
            "highest": "9℃",
            "wind": "北风",
            "windspeed": "0",
            "windsc": "0级",
            "sunrise": "06:47",
            "sunset": "16:54",
            "moonrise": "",
            "moondown": "",
            "pcpn": "0",
            "uv_index": "0",
            "aqi": "54",
            "quality": "良",
            "vis": "18",
            "humidity": "48",
            "alarmlist": [

            ],
            "tips": "晴天紫外线等级较高，外出注意补水防晒。天气凉，适宜着一到两件羊毛衫、大衣、毛套装、皮夹克等春秋着装；年老体弱者宜着风衣加羊毛衫等厚型春秋着装。空气质量较好，可以参与各类户外活动。疫情防控不松懈，出门请佩戴口罩。"
              }
            }
        * */
    }




}
